package com.star.bookstore.member.service.impl;

import com.star.bookstore.common.utils.R;
import com.star.bookstore.member.dao.AdminDao;
import com.star.bookstore.member.entity.AdminEntity;
import com.star.bookstore.member.entity.UserLogEntity;
import com.star.bookstore.member.service.AdminService;
import com.star.bookstore.member.service.UserLogService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.sql.Wrapper;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.star.bookstore.common.utils.PageUtils;
import com.star.bookstore.common.utils.Query;

import com.star.bookstore.member.dao.UserDao;
import com.star.bookstore.member.entity.UserEntity;
import com.star.bookstore.member.service.UserService;
import org.springframework.web.bind.annotation.RequestBody;

import javax.validation.Valid;


@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {

    @Autowired
    UserDao userDao;

    @Autowired
    UserService userService;

    @Autowired
    UserLogService userLogService;

    @Autowired
    AdminService adminService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        String key = (String) params.get("key");

        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(key)){

            Integer status = null;
            if (key.equals("在线")){
                status = 1;
            }else if (key.equals("离线")){
                status = 0;
            }
            wrapper.eq("user_id",key).or().like("username",key).or()
                    .like("mobile",key).or().like("login_status",status);
        }
        IPage<UserEntity> page = this.page(
                new Query<UserEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    //登录检查
    @Override
    public UserEntity loginCheck(String username, String password) {

        UserEntity userEntity = null;
        AdminEntity adminEntity = adminService.getById(1);
        System.out.println(adminEntity);
        //如果是管理员，则返回userId=0的userEntity
        if (username.equals(adminEntity.getAccount()) && password.equals(adminEntity.getPassword())){
            System.out.println("验证成功");
            //设置管理登录状态为真
            adminEntity.setLoginStatus(true);
            adminService.updateById(adminEntity);
            userEntity = new UserEntity();
            userEntity.setUserId(0L);
            return userEntity;
        }
        if (username.length() == 11){
            String mobile = username;
            userEntity = userDao.loginCheckByMobile(mobile,password);
        }else {
            userEntity = userDao.loginCheckByUsername(username, password);
        }
        //插入登录日志,并记用户登录状态真为
        if (userEntity != null){
            userEntity.setLoginStatus(true);
            userService.updateById(userEntity);
            UserLogEntity userLogEntity = new UserLogEntity();
            userLogEntity.setLogUserId(userEntity.getUserId());
            Timestamp timestamp = new Timestamp(new Date().getTime());
            userLogEntity.setLoginTime(timestamp);
            userLogService.save(userLogEntity);
        }
        return userEntity;
    }

    //注册
    @Override
    public boolean checkRepeat(@Valid UserEntity userEntity) {

        String username = userEntity.getUsername();
        String mobile = userEntity.getMobile();
        //根据用户名和手机号查重
        UserEntity userEntity1 = userDao.getByUsernameOrMobile(username,mobile);
        if (userEntity1 != null){
            return false;
        }else{
            return true;
        }
    }


}